
clear all
set more off, perm
capture log close
// set trace on
global klmChinaReach2: env klmChinaReach2
global datapath "${klmChinaReach2}/Fuyao_Erlfang"

cd "$datapath/Fuyao_trial/Task Pattern/art_passrate"

use "$datapath/data_task_panel_cluster_Feb24.dta", clear

capture drop temp
forval i=1/20{
	gen artc2_`i'=.
}

forval i=1/13{
	forval j=1/11{
		sum task if levelc2==`i' & repc2==`j'
		if (r(N)>0){
		   gen temp=task if levelc2==`i' & repc2==`j'
		   bys KIDID levelc2: egen temp2=max(temp)
		   replace artc2_`j'= temp2 if indc2==1 & levelc2==`i'
		   drop temp temp2
		}
	}
}

forval i=1/6{
	gen artf2_`i'=.
}

forval i=1/7{
	forval j=1/6{
		sum task if levelf2==`i' & repf2==`j'
		if (r(N)>0){
		   gen temp=task if levelf2==`i' & repf2==`j'
		   bys KIDID levelf2: egen temp2=max(temp)
		   replace artf2_`j'= temp2 if indf2==1 & levelf2==`i'
		   drop temp temp2
		}
	}
}

forval i=1/3{
	gen artg_`i'=.
}

forval i=1/9{
	forval j=1/3{
		sum task if levelg==`i' & repg==`j'
		if (r(N)>0){
		   gen temp=task if levelg==`i' & repg==`j'
		   bys KIDID levelg: egen temp2=max(temp)
		   replace artg_`j'= temp2 if indg==1 & levelg==`i'
		   drop temp temp2
		}
	}
}



// foreach i of var artl1-artl20{
// 	gen `i'_N = `i'
// 	gen `i'_se = `i'
// }
// order artl*_N, before(artl1_se)
// preserve
// collapse (count) artl1_N-artl20_N (mean) artl1-artl20 (sem) artl1_se-artl20_se if indl == 1, by(levell)
// save artl_passrate.dta, replace
// restore

foreach i of var artc2_1-artc2_20{
	gen `i'_N = `i'
	gen `i'_se = `i'
}
order artc2*_N, before(artc2_1_se)
preserve
collapse (count) artc2_1_N-artc2_20_N (mean) artc2_1-artc2_20 (sem) artc2_1_se-artc2_20_se if indc2 == 1, by(levelc2)
save artc2_passrate.dta, replace
restore

foreach i of var artf2_1-artf2_6{
	gen `i'_N = `i'
	gen `i'_se = `i'
}
order artf2*_N, before(artf2_1_se)
preserve
collapse (count) artf2_1_N-artf2_6_N (mean) artf2_1-artf2_6 (sem) artf2_1_se-artf2_6_se if indf2 == 1, by(levelf2)
save artf2_passrate.dta, replace
restore

foreach i of var artg_1-artg_3{
	gen `i'_N = `i'
	gen `i'_se = `i'
}
order artg*_N, before(artg_1_se)
preserve
collapse (count) artg_1_N-artg_3_N (mean) artg_1-artg_3 (sem) artg_1_se-artg_3_se if indg == 1, by(levelg)
save artg_passrate.dta, replace
restore

****Language****
use artl_passrate.dta, clear

drop if levell == 1

foreach i of varlist artl1-artl20{
	rename `i' `i'_m
}

foreach i of num 1/20{
	rename artl`i'_m artl_m`i'
	rename artl`i'_N artl_N`i'
	rename artl`i'_se artl_se`i'
}

reshape long artl_N artl_m artl_se, i(levell) j(task_order)

drop if artl_N == 0
drop if artl_N < 20

gen index = _n
gen ci_l = artl_m - 1.96*artl_se
gen ci_h = artl_m + 1.96*artl_se


drop if index == _N
twoway (line artl_m index, sort lcolor(navy)) (line ci_l index, sort lcolor(gray) lpattern(dash)) ///
(line ci_h index, sort lcolor(gray) lpattern(dash)), ytitle(Fraction of Completing the Task Independently) ///
ylabel(#3) xtitle(Language Difficulty Level) xline(11 16 22 28 38 55 70 77 86, lcolor(khaki)) ///
xlabel(11 16 22 28 38 55 70 77 86) title(Average Language Task Passing Rate by Order and Level)


****Cognitive****
use artc2_passrate.dta, clear


foreach i of varlist artc2_1-artc2_20{
	rename `i' `i'_m
}

foreach i of num 1/20{
	rename artc2_`i'_m artc2_m`i'
	rename artc2_`i'_N artc2_N`i'
	rename artc2_`i'_se artc2_se`i'
}

reshape long artc2_N artc2_m artc2_se, i(level) j(task_order)

drop if artc2_N < 20

gen index = _n
gen ci_l = artc2_m - 1.96*artc2_se
gen ci_h = artc2_m + 1.96*artc2_se

twoway (line artc2_m index, sort lcolor(navy)) (line ci_l index, sort lcolor(gray) lpattern(dash)) ///
(line ci_h index, sort lcolor(gray) lpattern(dash)), ytitle(Fraction of Completing the Task Independently) ///
ylabel(#3) xtitle(Cognitive Difficulty Level) xline(7 15 17 19 29 36 47 54 56 60 66 69, lcolor(khaki)) ///
xlabel(7 15 17 19 29 36 47 54 56 60 66 69) title(Average Cognitive Task Passing Rate by Order and Level)





// save artc2_passrate, replace

****Fine Motor****
use artf2_passrate.dta, clear


foreach i of varlist artf2_1-artf2_6{
	rename `i' `i'_m
}

foreach i of num 1/6{
	rename artf2_`i'_m artf2_m`i'
	rename artf2_`i'_N artf2_N`i'
	rename artf2_`i'_se artf2_se`i'
}

reshape long artf2_N artf2_m artf2_se, i(level) j(task_order)

drop if artf2_N < 20

gen index = _n
gen ci_l = artf2_m - 1.96*artf2_se
gen ci_h = artf2_m + 1.96*artf2_se

label var artf2_m "Task Passing Rate"
label var ci_l "95% Confidence Interval"

twoway (line artf2_m index, sort lcolor(navy)) (line ci_l index, sort lcolor(gray) lpattern(dash)) ///
(line ci_h index, sort lcolor(gray) lpattern(dash)), ytitle(Fraction of Completing the Task Independently, size(small)) ///
ylabel(0(0.5)1, labsize(small)) xtitle(Fine Motor Difficulty Level, size(small)) xline(6 8 14 20 23 27 30, lcolor(khaki) lwidth(medthin)) ///
xlabel(6 8 14 20 23 27 30, labsize(small)) title(Average Fine Motor Task Passing Rate by Order and Level, size(med))  ///
legend(order(1 2) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
note("Note: The yellow solid lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by the order of the children taking them.", size(small) span) ///
graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) xmtick(, labsize(small)) ymtick(, labsize(small)) ///
plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) 

drop if level == 7

twoway (line artf2_m index, sort lcolor(navy)) (line ci_l index, sort lcolor(gray) lpattern(dash)) ///
(line ci_h index, sort lcolor(gray) lpattern(dash)), ytitle(Fraction of Completing the Task Independently) ///
ylabel(#3) xtitle(Fine Motor Difficulty Level) xline(6 8 14 20 23, lcolor(khaki)) ///
xlabel(6 8 14 20 23) title(Average Fine Motor Task Passing Rate by Order and Level)
*/




****Gross Motor****

use artg_passrate.dta, clear


foreach i of varlist artg_1-artg_3{
	rename `i' `i'_m
}

foreach i of num 1/3{
	rename artg_`i'_m artg_m`i'
	rename artg_`i'_N artg_N`i'
	rename artg_`i'_se artg_se`i'
}

reshape long artg_N artg_m artg_se, i(level) j(task_order)

drop if artg_N < 20

gen index = _n
gen ci_l = artg_m - 1.96*artg_se
gen ci_h = artg_m + 1.96*artg_se

twoway (connected artg_m index, sort lcolor(navy)) (line ci_l index, sort lcolor(gray) lpattern(dash)) ///
(line ci_h index, sort lcolor(gray) lpattern(dash)), ytitle(Fraction of Completing the Task Independently) ///
ylabel(#3) xtitle(Gross Motor Difficulty Level) xline(2 3 6 8 10 13 16, lcolor(khaki)) ///
xlabel(2 3 6 8 10 13 16) title(Average Gross Motor Task Passing Rate by Order and Level)




